package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.PathManager;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICaseService;
import com.server.service.ICommonDataService;
import com.server.service.ICourseManageService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

/**
 * 课程编制
 * @author wufeng
 * @date 创建时间：2016年7月14日 下午3:53:18 
 */
@Controller
@RequestMapping("class")
public class CourseManageController {
	
	@Autowired
	private ICourseManageService courseManageService;
	@Autowired
	private ICommonDataService commonDataService;
	@Autowired
	private ICaseService caseService;
	
	@Login
	@RequestMapping("")
	public ModelAndView mv(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("courseMaking/courseList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	@Login
	@RequestMapping("addMv")
	public ModelAndView addMv(){
		ModelAndView mv = new ModelAndView("courseMaking/course_add");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@Login
	@RequestMapping("contentMv/{type}/{course_id}")
	public ModelAndView contentMv(HttpServletRequest request,@PathVariable Integer type,@PathVariable String course_id){
		ModelAndView mv = null;
		if(type==1){
			mv=new ModelAndView("courseMaking/course_add");
		}else{
			mv=new ModelAndView("courseMaking/update_course_make");
		}
				
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("course_id", course_id);
		Map<String, Object> course_name = courseManageService.getCourseInfo(course_id);
		mv.addObject("course_name",course_name.get("class_name"));
		mv.addObject("type",course_name.get("type"));
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		
		//mv.addObject("fileServer",PathManager.FILE_SERVER_ATTR);
		return mv;
	}
	@RequestMapping("checkMv/{course_id}")
	public ModelAndView checkMv(HttpServletRequest request,@PathVariable String course_id){
		ModelAndView mv = new ModelAndView("courseMaking/course_info");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("course_id", course_id);
		Map<String, Object> course_name = courseManageService.getCourseInfo(course_id);
		mv.addObject("course_name",course_name.get("class_name"));
		mv.addObject("type",course_name.get("type"));
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		
		//mv.addObject("fileServer",PathManager.FILE_SERVER_ATTR);
		return mv;
	}
	@Login
	@RequestMapping("selectSourse/{type}/{course_id}")
	public ModelAndView selectSourse(@PathVariable Integer type,@PathVariable String course_id){
		ModelAndView mv = new ModelAndView("courseMaking/course_material_list");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("course_id", course_id);
		mv.addObject("type", type);
		return mv;
	}
	@Login
	@RequestMapping("course_info_add")
	public void course_info_add(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("data");
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		Map<String, Object> map = JSONSerielizer.toMap(json);
		Map<String, Object> reMap = new HashMap<String, Object>();
		reMap = courseManageService.add(map, user);
		String jsonsString = JSONSerielizer.toJson(reMap, null, null);
		OutputUtil.returnString(response, jsonsString);
	}
	/**
	* 添加目录及附件
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月9日 下午3:38:50 
	* @return  
	*/
	@Login
	@RequestMapping("addContent/{course_id}")
	public void addContent(HttpServletRequest request, HttpServletResponse response,@PathVariable String course_id){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String value = request.getParameter("value");
		String pid = request.getParameter("pid");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", value);
		map.put("courseId", course_id);
		map.put("pid", pid) ;
		boolean isSuccess = courseManageService.addContent(map, user);
		OutputUtil.returnSuccess(response, isSuccess);
		
	}
	
	
	/**
	* 更新
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月9日 下午3:38:31 
	* @return  
	*/
	@RequestMapping("update/{id}")
	public void update(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String id){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String json = request.getParameter("data");
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = courseManageService.updateContent(id, map, user);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("updateSource/{id}")
	public void updateSource(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable String id){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String json = request.getParameter("data");
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = courseManageService.updateSource(id, map, user);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	* 获取目录树信息
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月9日 下午3:38:11 
	* @return  
	*/
	@RequestMapping("loadTree/{course_id}")
	public void loadTree(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer course_id){
		List<Map<String, Object>> treeData = courseManageService.getTreeData(course_id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(treeData, null, null));
	}
	@Login
	@RequestMapping("course_info")
	public ModelAndView course_info(){
		ModelAndView mv = new ModelAndView("courseMaking/course_name");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("course_make/{type}/{course_id}")
	public ModelAndView course_make(HttpServletRequest request,@PathVariable Integer type,@PathVariable Integer course_id){
		ModelAndView mv = new ModelAndView("courseMaking/check_course_make");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		mv.addObject("type", type);
		mv.addObject("course_id", course_id);
		
		StringBuffer serverBuffer = new StringBuffer();
		String serverName = request.getServerName();
		int serverPort = request.getServerPort();
		serverBuffer.append("http://").append(serverName)
		.append(":").append(serverPort).append("/")
		.append(PathManager.FILE_MAPPING);
		mv.addObject("fileServer",serverBuffer.toString());
		
		//mv.addObject("fileServer",PathManager.FILE_SERVER_ATTR);
		return mv;
	}
	@RequestMapping("course_info/{course_id}")
	public void course_info(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer course_id){
		Map<String, Object> reMap = courseManageService.getCourseInfo(course_id.toString());
		Object sourceIdObject = reMap.get("class_source");
		Object pptIdObject = reMap.get("class_ppt");
		if (!StringUtils.isEmpty(sourceIdObject)||!StringUtils.isEmpty(pptIdObject)) {
			Map<String, Object> map1 = courseManageService.getCourseRes(sourceIdObject.toString());
			Map<String, Object> map2 = courseManageService.getCourseRes(pptIdObject.toString());
			reMap.put("sourceName", map1.get("source_name"));
			reMap.put("sourceId", map1.get("attId"));
			reMap.put("pptName", map2.get("source_name"));
			reMap.put("pptId", map2.get("attId"));
		}
		String jsonsString = JSONSerielizer.toJson(reMap, null, null);
		OutputUtil.returnString(response, jsonsString);
	}
	/**
	* 修改课程信息
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月21日 上午10:31:41 
	* @return  
	*/
	@RequestMapping("update_course_info/{course_id}")
	public void update_course_info(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer course_id){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String json = request.getParameter("data");
		Map<String, Object> map = JSONSerielizer.toMap(json);
		boolean isSuccess = courseManageService.update(course_id.toString(), map, user);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	* 获取课程列表
	* @author  作者 : wufeng
	* @date 创建时间：2016年9月21日 上午10:33:48 
	* @return  
	*/
	@RequestMapping("list/{state}")
	public void list(HttpServletRequest request,HttpServletResponse response,
			@PathVariable int state){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String userType = SessionUtil.getStringAttri(request, SessionData.levelKey_mng);
		DataVector<Map<String, Object>> listData =  courseManageService.getList(userType,state, user, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(),JSONSerielizer.toJson(listData.getList(),null,null));
	}
	
	/**
	* 获取课程目录的附件
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月9日 下午3:37:03 
	* @return  
	*/
	@RequestMapping("classInfoData/{type}/{classType}/{courseId}/{pid}")
	public void getClassInfoData(HttpServletRequest request, HttpServletResponse response
			,@PathVariable String courseId, @PathVariable String pid, @PathVariable String type,@PathVariable String classType){
		List<Map<String, Object>> reList = courseManageService.getSourseList(courseId, pid, type,classType);
		String listJsonStr = JSONSerielizer.toJson(reList, null, null);
		OutputUtil.returnString(response, listJsonStr);
	}
	
	/**
	* 删除课程
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月1日 下午5:04:22 
	* @return  
	*/
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,HttpServletResponse response){
		boolean isSuccess = true;
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String str = request.getParameter("data");
		String []id = str.split(",");
		Map<String, Object> map = new HashMap<String, Object>();
		boolean errorInfo = true;
		for (String string : id) {
			if (courseManageService.isRight(string, user)) {
				isSuccess = courseManageService.delete(string);
			}else { 
				errorInfo = false;
				isSuccess = false;
			}
		}
		map.put("isSuccess", isSuccess);
		map.put("errorInfo", errorInfo);
		String mapJson = JSONSerielizer.toJson(map, null, null); 
		OutputUtil.returnString(response, mapJson);
	}
	@RequestMapping("deleteDir/{id}")
	public void deleteDir(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		//String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		boolean isSuccess = courseManageService.deleteDir(id);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	/**
	* 提交评估
	* @author  作者 : wufeng
	* @date 创建时间：2016年8月1日 下午5:03:10 
	* @return  
	*/
	@RequestMapping("submit")
	public void submit(HttpServletRequest request,
			HttpServletResponse response){
		String user = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String str = request.getParameter("data");
		boolean isSuccess = true;
		//String []id = str.split(",");
		Integer estimate = courseManageService.toEstimate(str, user);
		isSuccess = estimate > 0;
		OutputUtil.returnSuccess(response, isSuccess);
		//OutputUtil.returnSuccess(response, isSuccess, errorInfo);
	}
	
	@RequestMapping("preview/{type}/{sourceId}")
	public ModelAndView preView(@PathVariable Integer type,
			@PathVariable String sourceId){
		ModelAndView mv = new ModelAndView();
		if(type == 3 ){
			mv = new ModelAndView("courseMaking/video");
		}
		mv.addObject("sourceId", sourceId);
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("typeList")
	public void getTypeList(HttpServletRequest request, HttpServletResponse response){
		List<Map<String, Object>> reList = courseManageService.getTypeList();
		String jsonStr = JSONSerielizer.toJson(reList, null, null);
		OutputUtil.returnString(response, jsonStr);
	}
}
